package com.thinksns.sociax.t4.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.thinksns.sociax.android.R;
import com.thinksns.sociax.t4.android.Thinksns;
import com.thinksns.sociax.t4.android.user.ActivityUserInfo_2;
import com.thinksns.sociax.t4.android.video.ToastUtils;
import com.thinksns.sociax.t4.component.GlideCircleTransform;
import com.thinksns.sociax.t4.model.ModelEventNotifyComment;
import com.thinksns.sociax.t4.model.ModelSearchUser;
import com.thinksns.sociax.t4.model.ModelUser;
import com.thinksns.sociax.t4.unit.UiUtils;
import com.thinksns.sociax.t4.unit.UnitSociax;
import com.thinksns.sociax.thinksnsbase.utils.TimeHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhiyicx on 2017/5/11.
 */

public class AdapterEventComment extends BaseAdapter {

    private LayoutInflater inflater;
    private Context mContext;
    private List<ModelEventNotifyComment> comments;
    protected boolean noData = false;
    protected boolean needFullScreen;
    protected UnitSociax mUnitSociax;

    public AdapterEventComment(Context context, List<ModelEventNotifyComment> comments) {
        mContext = context;
        inflater = LayoutInflater.from(context);
        mUnitSociax = new UnitSociax(context);
        if (comments == null)
            this.comments = new ArrayList<>();
    }

    public void resetData(List<ModelEventNotifyComment> comments) {
        this.comments.clear();
        addAll(comments);
    }

    public void setNoData() {
        this.comments.clear();
        this.comments.add(new ModelEventNotifyComment());
        noData = true;
    }

    public void setNoDataWithFullScreen() {
        needFullScreen = true;
        setNoData();
    }

    public void setHasData() {
        if (noData)
            this.comments.clear();
        noData = false;
    }

    public boolean getNoData() {
        return noData;
    }

    public void addAll(List<ModelEventNotifyComment> comments) {
        this.comments.addAll(comments);
        notifyDataSetChanged();
    }

    public void addItem(ModelEventNotifyComment comment) {
        this.comments.add(comment);
        notifyDataSetChanged();
    }

    public void removeItem(ModelEventNotifyComment comment) {
        this.comments.remove(comment);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return comments.size();
    }

    @Override
    public ModelEventNotifyComment getItem(int position) {
        return comments.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        CommentViewHolder holder = null;
        if (convertView == null || convertView.findViewById(R.id.item_no_data) != null) {
            convertView = inflater.inflate(R.layout.listitem_comment_list, null);
            holder = new CommentViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (CommentViewHolder) convertView.getTag();
        }
        if (noData) {
            holder.ll_empty.setVisibility(View.VISIBLE);
            holder.ll_content.setVisibility(View.GONE);
            return convertView;
        }
        holder.ll_empty.setVisibility(View.GONE);
        holder.ll_content.setVisibility(View.VISIBLE);
        final ModelEventNotifyComment comment = getItem(position);
        holder.tv_user_name.setText(TextUtils.isEmpty(comment.getRemark()) ? comment.getUname() : comment.getRemark());
        holder.tv_time.setText(TimeHelper.friendlyTime(getItem(position).getCtime() + ""));
        UiUtils.loadImage(mContext, new GlideCircleTransform(mContext), holder.iv_comment_user_face, comment.getAvatar(), R.drawable.default_user);
       /*认证图标 8.8.2017*/
        holder.ll_user_group.removeAllViews();
       if (comment.getUser_group() != null && !comment.getUser_group().isEmpty()){
           mUnitSociax.addUserGroup(comment.getUser_group(),holder.ll_user_group);
       }
        holder.iv_comment_user_face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (comment.getUid() != Thinksns.getMy().getUid() && comment.getSpace_privacy() == ModelUser.CAN_NOT_TO_HOME) {
                    ToastUtils.showToast(R.string.tip_no_oauth_to_other_home);
                    return;
                }
                Intent intent = new Intent(mContext, ActivityUserInfo_2.class);
                intent.putExtra("uid", getItem(position).getUid());
                mContext.startActivity(intent);
            }
        });
        holder.tv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((ListView) parent).performItemClick(v, position, position);
            }
        });
        mUnitSociax.showContentLinkViewAndLinkMovementchat(null, comment.getContent(), comment.getTo_uid(), holder.tv_content, 14);
//        UnitSociax.showContentLinkViewAndLinkMovement(getItem(position).getContent(), holder.tv_content);
        return convertView;
    }

    public class CommentViewHolder {
        ImageView iv_comment_user_face;
        TextView tv_user_name, tv_time, tv_content;
        LinearLayout ll_user_group;
        LinearLayout ll_empty; // 为空缺省文字
        LinearLayout ll_content;

        public CommentViewHolder(View itemView) {
            iv_comment_user_face = (ImageView) itemView.findViewById(R.id.img_comment_userface);
            tv_user_name = (TextView) itemView.findViewById(R.id.tv_comment_uname);
            tv_time = (TextView) itemView.findViewById(R.id.tv_comment_ctime);
            tv_content = (TextView) itemView.findViewById(R.id.tv_comment_content);
            ll_user_group = (LinearLayout) itemView.findViewById(R.id.ll_user_group);
            ll_empty = (LinearLayout) itemView.findViewById(R.id.ll_empty);
            ll_content = (LinearLayout) itemView.findViewById(R.id.ll_content);
        }
    }
}
